package com.hjq.fcloud.ui.adapter

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import com.hjq.fcloud.R
import com.hjq.fcloud.http.api.FinancingOverviewApi
import com.hjq.fcloud.ktx.ViewKtx.setVisibility
import com.hjq.fcloud.ktx.ViewKtx.setVisibility2
import com.zhy.adapter.abslistview.CommonAdapter
import com.zhy.adapter.abslistview.ViewHolder

/**
 * 融资需求全览抽屉adapter
 */
class FinancingOverviewDrawerAdapter(
    context: Context,
    datas: MutableList<FinancingOverviewApi.DataJson>
) : CommonAdapter<FinancingOverviewApi.DataJson>(
    context,
    R.layout.item_financing_overview_drawer,
    datas
) {


    var listener: FinancingOverviewDrawerListener? = null
    private var currentIndex: Int = 0

    override fun convert(
        viewHolder: ViewHolder,
        item: FinancingOverviewApi.DataJson,
        position: Int
    ) {
        var itemView = viewHolder.getView<View>(R.id.itemView)
        var titleTv = viewHolder.getView<TextView>(R.id.titleTv)
        var endLineView = viewHolder.getView<View>(R.id.endLineView)
        if (currentIndex == position) {
            itemView.setBackgroundColor(mContext.resources.getColor(R.color.line_color_pink_light))
            titleTv.setTextColor(mContext.resources.getColor(R.color.common_color))
            endLineView.setVisibility2(true)
        } else {
            itemView.setBackgroundColor(mContext.resources.getColor(R.color.white))
            titleTv.setTextColor(mContext.resources.getColor(R.color.common_text_color_333))
            endLineView.setVisibility2(false)
        }
        titleTv.setText(item.info?.trim())

        viewHolder.setText(R.id.corpNameTv, item.corp_name?.replace(",", "\n"))

        var createTimeTv = viewHolder.getView<TextView>(R.id.contentTv)
        createTimeTv.setText(item.create_time)
        createTimeTv.setVisibility(!TextUtils.isEmpty(item.create_time))

        var flowDotIv = viewHolder.getView<View>(R.id.flowDotIv)
        var flowDotLineView = viewHolder.getView<View>(R.id.flowDotLineView)
        if (item.enable?.equals("1") == true) {
            flowDotIv.setBackgroundResource(R.drawable.ic_flow_dot)
            flowDotLineView.setBackgroundColor(mContext.resources.getColor(R.color.line_color_pink))
            titleTv.setTextColor(mContext.resources.getColor(R.color.black))
        } else {
            flowDotIv.setBackgroundResource(R.drawable.ic_flow_dot_end)
            flowDotLineView.setBackgroundColor(mContext.resources.getColor(R.color.common_line_color))
            titleTv.setTextColor(mContext.resources.getColor(R.color.common_text_color_999))
        }
        itemView.setOnClickListener {
            if (item.enable?.equals("1") == true) {
                currentIndex = position
                notifyDataSetChanged()
                listener?.onSelect(position, item)
            }
        }
    }

    fun setFinancingOverviewDrawerListener(listener: FinancingOverviewDrawerListener) {
        this.listener = listener
    }

    fun setSelection(position: Int) {
        this.currentIndex = position
        notifyDataSetChanged()
    }
}

interface FinancingOverviewDrawerListener {
    fun onSelect(index: Int, item: Any)
}